package org.example.script.utils;

import org.example.model.CallBack;
import org.example.model.CallBackAndReturn;
import org.example.model.Device;
import org.example.utils.CommonUtils;
import org.example.utils.DateUtils;
import org.example.utils.GlobalUtils;
import org.openqa.selenium.WebDriver;

import java.util.Date;

public class SwipeExecutor {
    private WebDriver webDriver;

    private Device device;

    private String pageName;

    private CallBackAndReturn toPageFun;

    private CallBack checkPopupFun;
    /**
     * 等待最短时间：单位（秒）
     */
    private int minTime;
    /**
     * 等待最长时间：单位（秒）
     */
    private int maxTime;
    /**
     * 任务时间：单位（分钟）
     */
    private int durationTime;
    /**
     * 滑动类型
     */
    private SwipeEnum swipeEnum;

    private CallBack customSwipeMethod;

    private CallBack backToTaskPage;
    private CallBack customExecution;

    public SwipeExecutor() {}

    public void initParams(WebDriver webDriver, Device device, String pageName,
                           CallBackAndReturn toPageFun, CallBack checkPopupFun,
                           Integer minTime, Integer maxTime, Integer durationTime, SwipeEnum swipeEnum,
                           CallBack customSwipeMethod,
                           CallBack backToTaskPage, CallBack customExecution) {
        this.webDriver = webDriver;
        this.device = device;
        this.pageName = pageName;
        this.toPageFun = toPageFun;
        this.checkPopupFun = checkPopupFun;
        this.minTime = minTime;
        this.maxTime = maxTime;
        this.durationTime = durationTime;
        this.swipeEnum = swipeEnum;
        this.customSwipeMethod = customSwipeMethod;
        this.backToTaskPage = backToTaskPage;
        this.customExecution = customExecution;

        if (minTime == null || minTime <= 0) {
            this.minTime = 5000;
        }
        if (maxTime == null || maxTime <= 0) {
            this.maxTime = 10000;
        }
        if (durationTime == null || durationTime <= 0) {
            this.durationTime = 10;
        }
        if (swipeEnum == null) {
            this.swipeEnum = SwipeEnum.SWIPE_LEFT;
        }
    }

    public boolean execute() {
        if (!toPageFun.execute()) {
            GlobalUtils.logi("未进入"+pageName+"页面，"+pageName+"任务结束！");
            return false;
        }
        GlobalUtils.logi("已进入" + pageName + "页面");

        GlobalUtils.logi("总共用时" + durationTime + "分钟左右");
        Date futureDate = DateUtils.addMinuteTimes(durationTime);
        Date currentDate = new Date();
        while (currentDate.getTime() <= futureDate.getTime()) {
            if (checkPopupFun != null) {
                checkPopupFun.execute();
            }

            if (customExecution != null) {
                customExecution.execute();
            }

            // 如果有自定义滑动优先执行自定义滑动
            if (customSwipeMethod != null) {
                customSwipeMethod.execute();
            } else {
                GlobalUtils.logi("向" + swipeEnum.getSwipeName() + "滑动");
                if (swipeEnum.equals(SwipeEnum.SWIPE_LEFT)) {
                    CommonUtils.swipeLeft(webDriver, device);
                } else if (swipeEnum.equals(SwipeEnum.SWIPE_RIGHT)) {
                    CommonUtils.swipeRight(webDriver, device);
                } else if (swipeEnum.equals(SwipeEnum.SWIPE_UP)) {
                    CommonUtils.swipeUp(webDriver, device);
                } else if (swipeEnum.equals(SwipeEnum.SWIPE_DOWN)) {
                    CommonUtils.swipeDown(webDriver, device);
                }
            }

            long sleepTime = CommonUtils.randomSleep(minTime, maxTime);
            GlobalUtils.logi("剩余时间：" + (futureDate.getTime() - currentDate.getTime()) / 1000 / 60 % 60 + "分钟");

            currentDate = new Date();
        }

        GlobalUtils.logi("已执行" + durationTime + "分钟");

        if (backToTaskPage != null) {
            backToTaskPage.execute();
        }

        return true;
    }
}
